package cn.wsalix.order.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import cn.anlaser.entity.SimpleEntity;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.shop.entity.Price;
import cn.wsalix.shop.entity.Product;
import cn.wsalix.shop.entity.Shop;
import cn.wsalix.shop.status.PayEnum;
import cn.wsalix.status.OrderEnum;
import cn.wsalix.status.TuanEnum;
import cn.wsalix.type.OrderTypeEnum;
import cn.wsalix.type.PayTypeEnum;
import cn.wsalix.user.entity.UserInfo;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "ORDER")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@EntityListeners({ AuditingEntityListener.class })
@Data
@EqualsAndHashCode(exclude = { "masterTuan", "tuanMaster", "shop", "customer",
		"orderInfo", "price", "product", "tuanOrders" }, callSuper = false)
@ToString(exclude = { "masterTuan", "tuanMaster", "shop", "customer",
		"orderInfo", "price", "product", "tuanOrders"  })
public class Order extends SimpleEntity {
	/**
	 *
	 */
	private static final long serialVersionUID = 7245697631565191281L;
	private String tradeNo;// 订单号
	private String title;// 标题

	@Temporal(TemporalType.TIMESTAMP)
	private Date orderTime;
	private String orderDate;
	private OrderEnum status;// 订单状态
	private OrderTypeEnum orderType;

	private String remark;// 订单描述
	private Integer postfree;
	private int amount;//
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_SHOP"), name = "SHOP_ID")
	private Shop shop;
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_USER"), name = "USER_ID")
	private SysUser customer;// 购物人、团购发起人
	private TuanEnum tuanStatus;
	@JsonIgnore
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_MASTERTUAN"), name = "MASTER_TUAN_ID")
	private MasterTuan masterTuan;

	private boolean tuan;
	private boolean master;// 团购主订单标志
	private int inOnNum;
	
	// /
	@OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, mappedBy = "tuanMaster")
	private Set<Order> tuanOrders;

	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_TUAN"), name = "TUAN_ORDER_ID")
	private Order tuanMaster;
	// /
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_PRODUCT"), name = "PRODUCT_ID")
	private Product product;
	private int num;
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_PRICE"), name = "PRICE_ID")
	private Price price;

	//@OneToOne(optional = true, cascade = CascadeType.ALL)
	//@JoinColumn(name = "ORDER_INFO_ID")
	//@JoinColumn(name = "ID", referencedColumnName = "ORDER_ID", unique = true)
	@OneToOne(optional = true,cascade=CascadeType.MERGE, fetch = FetchType.EAGER) 
	@PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")
	private OrderInfo orderInfo;
	/*
	 * @ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch =
	 * FetchType.LAZY)
	 * 
	 * @JoinColumn(name = "MY_PRODUCT_ID") private MyProduct myProduct;
	 */
	@Transient
	private UserInfo userInfo;
	private String customerName;// masterName;// 主团购人名字
	private String avatar;// 购买人头像
	//
	private PayTypeEnum payType;
	private PayEnum pay;

	private String payTypeTitle;
	private String payTitle;

	private String phone;
	private String address;
	private String zipCode;

	private String express;// 快递
	private String expressNo;// 快递编号
	private String expressPhone;// 快递电话
	@Transient
	private String noticeBody;// 快递电话
}